//
//  TTInputAlertView.m
//  TaoTuo
//
//  Created by admin on 2019/12/13.
//  Copyright © 2019 admin. All rights reserved.
//

#import "LLInputAlertView.h"
#import <UIView+TYAlertView.h>

@interface LLInputAlertView ()<YYTextViewDelegate>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) YYTextView *textView;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UILabel *countLabel;  //统计字数的
@property (nonatomic, strong) UIButton *confirmBtn;

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *content;
@property (nonatomic, assign) int maxTextCount;
@property (nonatomic, copy) ConfirmBlock confirmBlock;

@end

@implementation LLInputAlertView

+ (instancetype)inputViewWithTitle:(NSString *)title content:(NSString *)content maxTextCount:(int)maxTextCount confirmBlock:(ConfirmBlock)confirmBlock
{
    LLInputAlertView *view = [[self alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 50, 200)];
    view.title = title;
    view.content = [content removeFirstAndLastBlank];
    view.maxTextCount = maxTextCount;
    view.confirmBlock = confirmBlock;
    
    [view setupSubviews];
    
    [[NSNotificationCenter defaultCenter] addObserver:view selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:view selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
    
    return view;
}

- (void)keyboardWillShow:(NSNotification *)notify
{
    NSDictionary *dic = notify.userInfo;
    NSLog(@"%@", dic);
    NSValue *value = dic[UIKeyboardFrameEndUserInfoKey];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.bottom = [value CGRectValue].origin.y;
    }];
}

- (void)keyboardWillHidden:(NSNotification *)notify
{
    [UIView animateWithDuration:0.25 animations:^{
        self.center = self.superview.center;
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}

///取消按钮点击了
- (void)cancelAction
{
    [self hideInWindow];
}

///确定按钮点击了
- (void)confirmAction
{
    if (self.confirmBlock) {
        self.confirmBlock(self.textView.text);
    }
    [self hideInWindow];
}

- (BOOL)textView:(YYTextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (text.length == 0) {
        return YES;
    } else {
        return _content.length <= self.maxTextCount;
    }
}

- (void)setupSubviews
{
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 10;
    
    _titleLabel = [UILabel labelTextCenterWithTitle:self.title color:UIColorHex(0E0E0E) fontSize:15];
    _titleLabel.frame = CGRectMake(0, 0, self.width, 50);
    [self addSubview:_titleLabel];
    
    _textView = [[YYTextView alloc] initWithFrame:CGRectMake(10, _titleLabel.bottom, self.width - 20, 100)];
    _textView.delegate = self;
    _textView.backgroundColor = [UIColor whiteColor];
    _textView.placeholderText = @"请输入备注内容";
    _textView.placeholderTextColor = UIColorHex(DEDEDE);
    _textView.placeholderFont = [UIFont systemFontOfSize:13];
    _textView.font = [UIFont systemFontOfSize:15];
    _textView.textColor = UIColorHex(333333);
    _textView.text = self.content;
    [self addSubview:_textView];
    
    NSString *str = [NSString stringWithFormat:@"%ld/%d", _content.length, self.maxTextCount];
    _countLabel = [UILabel labelWithTitle:str color:UIColorHex(999999) font:[UIFont systemFontOfSize:13] alignment:NSTextAlignmentRight];
    _countLabel.frame = CGRectMake(20, _textView.bottom - 20, _textView.width - 10, 20);
    [self addSubview:_countLabel];
    
    [_textView addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
    
    [LLLineUtilies addLineInView:self startPoint:CGPointMake(0, _textView.bottom) endPoint:CGPointMake(self.width, _textView.bottom) lineColor:UIColorHex(#EBEBEB) lineWidth:0.5];
    [LLLineUtilies addLineInView:self startPoint:CGPointMake(self.width * 0.5, _textView.bottom) endPoint:CGPointMake(self.width * 0.5, self.bottom) lineColor:UIColorHex(#EBEBEB) lineWidth:0.5];
    
    _cancelBtn = [UIButton createButtonWithTitle:@"取消" titleColor:UIColorHex(5A5A5A) titleFont:[UIFont systemFontOfSize:13] backgroundColor:nil target:self action:@selector(cancelAction)];
    _cancelBtn.frame = CGRectMake(0, _textView.bottom, self.width * 0.5, 50);
    [self addSubview:_cancelBtn];
    
    _confirmBtn = [UIButton createButtonWithTitle:@"确定" titleColor:UIColorHex(6822B7) titleFont:[UIFont systemFontOfSize:13] backgroundColor:nil target:self action:@selector(confirmAction)];
    _confirmBtn.frame = CGRectMake(self.width * 0.5, _textView.bottom, self.width * 0.5, 50);
    [self addSubview:_confirmBtn];
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    _content = change[NSKeyValueChangeNewKey];
    _countLabel.text = [NSString stringWithFormat:@"%ld/%d", _content.length, self.maxTextCount];
}


- (void)dealloc
{
    [_textView removeObserver:self forKeyPath:@"text"];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    NSLog(@"[< %@ >] --> 释放了", [self className]);
}


@end
